package com.breakfast.fun.adapter.mall;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.breakfast.fun.R;
import com.breakfast.fun.base.BaseListAdapter;
import com.breakfast.fun.dao.DaoMaster;
import com.breakfast.fun.dao.GoodCart;
import com.breakfast.fun.dao.GoodCartDao;
import com.breakfast.fun.event.GoodEvent;
import com.breakfast.fun.event.MallDetailEvent;
import com.breakfast.fun.util.L;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * Created by yukai on 16/9/16.
 */
public class CartGoodAdapter extends BaseListAdapter<GoodCart> {

    private GoodCartDao mGoodCartDao;
    private QueryBuilder qb;

    private List<GoodCart> mGoodCarts;
    private int totalNum = 0;
    private double totalPrice = 0;
    private GoodEvent event = new GoodEvent();

    public CartGoodAdapter(Context context, List<GoodCart> list) {
        super(context, list);
        mGoodCartDao = DaoMaster.newDevSession(context, "zaofan.db").getGoodCartDao();
        qb = mGoodCartDao.queryBuilder();
        qb.LOG_SQL = true;
        qb.LOG_VALUES = true;
        mGoodCarts = mGoodCartDao.loadAll();
        for (int i = 0; i < mGoodCarts.size(); i++) {
            totalPrice += mGoodCarts.get(i).getTotalPrice();
            totalNum += mGoodCarts.get(i).getCount();
        }
        event.setTotalPrice(totalPrice);
        event.setTotalNum(totalNum);
        EventBus.getDefault().post(event);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_cart_good, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        GoodCart data = getItem(position);
        holder.mTvCartGoodName.setText(data.getName());
        holder.mTvCartGoodPrice.setText("￥" + data.getPrice() + "/份");
        holder.mTvCartGoodCount.setText(data.getCount() + "");

        holder.mBtnCartGoodAdd.setOnClickListener(v -> {
            addProduct(data, holder);
        });

        holder.mBtnCartGoodDel.setOnClickListener(v -> {
            delProduct(data, holder);
        });

        imageUtil.loadUrlImage(data.getImage(), holder.mIvCartGood);
        L.d("cart     " + data.getImage());
        return convertView;
    }


    private void delProduct(GoodCart data, ViewHolder holder) {
        List<GoodCart> goodCarts = getGoodList(data.getGoodId());

        goodCarts.get(0).setCount(goodCarts.get(0).getCount() - 1);
        goodCarts.get(0).setTotalPrice(goodCarts.get(0).getPrice() * goodCarts.get(0).getCount());
        mGoodCartDao.update(goodCarts.get(0));
        holder.mTvCartGoodCount.setText(goodCarts.get(0).getCount() + "");
        totalNum -= 1;
        totalPrice -= data.getPrice();
        event.setTotalNum(totalNum);
        event.setTotalPrice(totalPrice);
        EventBus.getDefault().post(event);

        if (goodCarts.get(0).getCount() == 0) {
            mGoodCartDao.delete(goodCarts.get(0));
            removeEntity(data);
            notifyDataSetChanged();
        }
        EventBus.getDefault().post(new MallDetailEvent(goodCarts.get(0).getCount()));
    }

    private void addProduct(GoodCart data, ViewHolder holder) {
        List<GoodCart> goodCarts = getGoodList(data.getGoodId());


        goodCarts.get(0).setCount(goodCarts.get(0).getCount() + 1);
        goodCarts.get(0).setTotalPrice(goodCarts.get(0).getPrice() * goodCarts.get(0).getCount());
        mGoodCartDao.update(goodCarts.get(0));
        holder.mTvCartGoodCount.setText(goodCarts.get(0).getCount() + "");
        totalNum += 1;
        totalPrice += data.getPrice();
        event.setTotalNum(totalNum);
        event.setTotalPrice(totalPrice);
        EventBus.getDefault().post(event);
    }


    public List<GoodCart> getGoodList(int goodId) {
        qb = mGoodCartDao.queryBuilder();
        Query query = qb.where(
                GoodCartDao.Properties.GoodId.eq(goodId))
                .build();
        return query.list();
    }

    static class ViewHolder {
        @Bind(R.id.tv_cart_item_date_hint)
        TextView mTvCartItemDateHint;
        @Bind(R.id.iv_cart_good)
        ImageView mIvCartGood;
        @Bind(R.id.tv_cart_good_name)
        TextView mTvCartGoodName;
        @Bind(R.id.tv_cart_good_price)
        TextView mTvCartGoodPrice;
        @Bind(R.id.btn_cart_good_add)
        Button mBtnCartGoodAdd;
        @Bind(R.id.tv_cart_good_count)
        TextView mTvCartGoodCount;
        @Bind(R.id.btn_cart_good_del)
        Button mBtnCartGoodDel;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
